using System;
using System.Collections.Generic;
using System.Text;
using Decav.Gatsb.Application;
using System.Web;

namespace Decav.Gatsb.Web
{
    /// <summary>
    /// Manages Gatsb Web Application instances through the session state.
    /// </summary>
    public class GatsbWebApplicationProvider : IGatsbApplicationProvider
    {
        private const string SessionAppInstance = "_GatsbAppInstance";
        private static readonly object _syncRoot = new object();

        #region IGatsbApplicationProvider Members

        public GatsbApplication GetCurrentApplicationInstance()
        {
            lock (_syncRoot)
            {
                GatsbApplication app = HttpContext.Current.Session[SessionAppInstance] as GatsbApplication;
                if (app == null)
                    HttpContext.Current.Session[SessionAppInstance] = app = new GatsbWebApplication();

                return app;
            }
        }

        #endregion
    }
}
